﻿using ABCNet.Authorization.Users;
using Abp.Configuration.Startup;
using Abp.Dependency;
using Abp.MultiTenancy;
using Abp.Runtime;
using Abp.Runtime.Session;
using System;
using System.Linq;
using System.Security.Claims;

namespace ABCNet.Extensions
{
    /// <summary>
    /// 对AbpSession进行扩展
    /// </summary>
    public class ABCNetSession : ClaimsAbpSession, IABCNetSession, ISingletonDependency
    {
        public ABCNetSession(IPrincipalAccessor principalAccessor, IMultiTenancyConfig multiTenancy, ITenantResolver tenantResolver, IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) 
            : base(principalAccessor,multiTenancy, tenantResolver,sessionOverrideScopeProvider)
        {

        }

        public virtual AccountTypes AccountType
        {
            get
            {
                var accountTypeClaim = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == "AccountType");
                if (string.IsNullOrEmpty(accountTypeClaim?.Value))
                    return AccountTypes.会员账户;
                if (int.TryParse(accountTypeClaim.Value, out int accountType))
                {
                    return (AccountTypes)accountType;
                }
                return AccountTypes.会员账户;
            }
        }
    }
}
